﻿using PayrollHR.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic;

namespace PayrollHR.Administration.Modules
{
    public partial class ResultControl : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.SetDefaul();
            }
        }
        protected void SetDefaul()
        {
            //ctrSearch.SetDefaul();
        }
        protected void BindGrid(ref List<Results> List)
        {
            var resultSearch = GetSearchResult();
            List = this.ResultService.GetAllResult(packageCode: resultSearch.packageCode, fromdate: resultSearch.From, todate: resultSearch.To, Distributor : resultSearch.Distributor,
                customerName: resultSearch.CustName, taxcode: resultSearch.TaxCode, mobile: resultSearch.CustMobile, email: resultSearch.CustEmail, confrim : resultSearch.Confirm);
        }
        protected void Bind(List<Results> List)
        {
            gvMail.DataSource = List;
            gvMail.DataBind();
        }
        protected void Bind()
        {
            List<Results> List = new List<Results>();
            this.BindGrid(ref List);
            this.Bind(List);
        }
        #region btnEvent
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            this.Bind();
        }

        protected void gvMail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMail.PageIndex = e.NewPageIndex;
            this.Bind();
        }
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            this.SendQueued();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this.Delete();
        }
        #endregion
        #region SendMail
        protected void SendQueued()
        {
            int Row = gvMail.Rows.Count;
            string MessageID = "";
            string custommer = "";
            try
            {
                foreach (GridViewRow row in gvMail.Rows)
                {
                    var cbResult = row.FindControl("cbResult") as CheckBox;
                    var hfResultsId = row.FindControl("hfResultsId") as HiddenField;
                    var hfCustomerId = row.FindControl("hfCustomerId") as HiddenField;
                    bool isChecked = cbResult.Checked;
                    if (isChecked)
                    {
                        MessageID += hfResultsId.Value + ",";
                        custommer += hfCustomerId.Value + ",";
                    }
                }
                if (MessageID.Length > 0)
                {
                    MessageID = MessageID.Substring(0, MessageID.Length - 1);
                    custommer = custommer.Substring(0, custommer.Length - 1);
                    this.MessageService.SaveQueuedEmail(MessageID, HttpContext.Current.Request.PhysicalApplicationPath);
                }
                this.Bind();
            }
            catch (Exception ex)
            {
                ProcessException(ex);
            }
        }
        #endregion
        #region Function
        /// <summary>
        /// Gets the search result.
        /// </summary>
        /// <returns></returns>
        private CustomerSearchResult GetSearchResult()
        {
            return new CustomerSearchResult
            {
                CustName = txtCustName.Text.Trim(),
                PackageName = txtPackName.Text.Trim(),
                packageCode = txtMaCode.Text.Trim(),
                CustEmail = txtCustEmail.Text.Trim(),
                CustMobile = txtMobile.Text.Trim(),
                TaxCode = txtTaxcode.Text.Trim(),
                From = dpFromdate.SelectText,
                To = dpTodate.SelectText,
                Confirm = ckConfirm.Checked ? 2 : 3,
                Distributor = txtCustomerName.Text.Trim(),
                UserId = PayrollContext.Current.Session.EmployeeId
            };
        }
        /// <summary>
        /// Deletes this instance.
        /// </summary>
        protected void Delete()
        {
            if (Page.IsValid)
            {
                int Row = gvMail.Rows.Count;
                string result = "";
                try
                {
                    foreach (GridViewRow row in gvMail.Rows)
                    {
                        var cbResult = row.FindControl("cbResult") as CheckBox;
                        var hfResultsId = row.FindControl("hfResultsId") as HiddenField;
                        bool isChecked = cbResult.Checked;
                        if (isChecked)
                        {
                            result += hfResultsId.Value + ",";
                        }
                    }
                    if (result.Length > 0)
                    {
                        result = result.Substring(0, result.Length - 1);

                        var resultSearch = GetSearchResult();
                        List<Results> List = this.ResultService.Delete(resultId: result, packageCode: resultSearch.packageCode, fromdate: resultSearch.From, todate: resultSearch.To, Distributor : resultSearch.Distributor,
                            customerName: resultSearch.CustName, taxcode: resultSearch.TaxCode, mobile: resultSearch.CustMobile, email: resultSearch.CustEmail);
                        this.Bind(List);
                    }
                }
                catch (Exception ex)
                {
                    ProcessException(ex);
                }
            }
        }
        #endregion

        #region grid event
        protected void gvMail_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)gvMail.Rows[e.RowIndex];
            int id = int.Parse(gvMail.DataKeys[e.RowIndex].Value.ToString());
            TextBox txtCustomerName = (TextBox)row.FindControl("txtCustomerName");
            TextBox txtTaxCode = (TextBox)row.FindControl("txtTaxCode");
            TextBox txtMobile = (TextBox)row.FindControl("txtMobile");
            TextBox txtDatePurchase = (TextBox)row.FindControl("txtDatePurchase");
            TextBox txtPackageCode = (TextBox)row.FindControl("txtPackageCode");
            TextBox txtCustomerProfile = (TextBox)row.FindControl("txtCustomerProfile");
            this.ResultService.Save(new Results
            {
                ResultsId = id,
                PackageCode = txtPackageCode.Text.Trim(),
                DatePurchase = txtDatePurchase.Text.Trim(),
                TaxCode = txtTaxCode.Text.Trim(),
                Mobile = txtMobile.Text.Trim(),
                CustomerName = txtCustomerName.Text.Trim(),
                CustomerProfile = txtCustomerProfile.Text.Trim()
            });
            gvMail.EditIndex = -1;
            this.Bind();
        }

        protected void gvMail_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvMail.EditIndex = e.NewEditIndex;
            this.Bind();
        }

        protected void gvMail_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvMail.EditIndex = -1;
            this.Bind();
        }
        #endregion
    }
}